package com.group.controller;


import com.group.pojo.ProductOrder;
import com.group.service.ProductOrderService;
import com.group.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author group
 * @since 2021-09-06
 */
@RestController
@RequestMapping("/group/product-order")
@Api
public class ProductOrderController {
    @Autowired
    ProductOrderService productOrderService;

    @ApiOperation(value = "根据用户id获取订单")
    @GetMapping("getOrderByUserId")
    public R getOrderByUserId(Integer id) {
        System.out.println(id);
        List<ProductOrder> list = productOrderService.getOrderByUserId(id);
        return R.ok().data("list", list);
    }

    @ApiOperation(value = "根据商品id获取订单")
    @GetMapping("getOrderByProductId")
    public R getOrderByProductId(Integer id) {
        System.out.println(id);
        List<ProductOrder> list = productOrderService.getOrderByProductId(id);
        return R.ok().data("list", list);
    }

    @ApiOperation(value = "新增订单")
    @PostMapping("insertOrder")
    public R insertOrder(@RequestBody ProductOrder productOrder) {
        boolean flag = productOrderService.insertOrder(productOrder);
        return flag ? R.ok() : R.error();
    }


    @ApiOperation(value = "更新订单状态")
    @DeleteMapping("{id}")
    public R deleteProductOrder(@PathVariable("id") int id) {
        boolean flag = productOrderService.deleteProductOrder(id);
        return flag ? R.ok() : R.error();
    }

    @ApiOperation(value = "更新订单")
    @PostMapping("updateOrder")
    public R updateOrder(@RequestBody  ProductOrder productOrder) {
        boolean flag = productOrderService.updateOrder(productOrder);
        return flag ? R.ok() : R.error();
    }
}

